package com.chdwmp.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.chdwmp.product.bean.PageResult;
import com.chdwmp.product.bean.Product;
import com.chdwmp.product.bean.backend.ProductSearchVo;
import com.chdwmp.product.constant.CommonConstants;
import com.chdwmp.product.constant.Status;
import com.chdwmp.product.exception.CommonException;
import com.chdwmp.product.mapper.ProductMapper;
import com.chdwmp.product.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Auther: LLL
 * @Date: 2021/3/6 20:52
 * @Description: 产品业务实现类
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Override
    public void addProduct(Product product) {
        int count =  productMapper.queryProductIsExits(product.getProductName());
        if(count > 0)
            throw new CommonException(Status.PRODUCT_IS_EXITS);
        productMapper.addProduct(product);
    }

    @Override
    public void updateProduct(Product product) {
        if(ObjectUtil.isNull(product.getId()))
            throw new CommonException(Status.MISS_REQUIRED_PARAMETER);
        int count =  productMapper.queryProductIsExitsForUpdate(product.getProductName(),product.getId());
        if(count > 0)
            throw new CommonException(Status.PRODUCT_IS_EXITS);
        productMapper.updateProduct(product);
    }

    @Override
    public void deleteProduct(String id) {
        if(ObjectUtil.isNull(id))
            throw new CommonException(Status.MISS_REQUIRED_PARAMETER);
        productMapper.deleteProduct(id);
    }

    @Override
    public PageResult getProductList(ProductSearchVo productSearchVo) {

        int rows = productSearchVo.getPageSize();
        if (rows == 0) {
            rows = CommonConstants.PAGE_SIZE;
        }
        int start_rows = productSearchVo.getPage() * rows;
        productSearchVo.setPage(start_rows);
        productSearchVo.setPageSize(rows);

        if(StringUtils.isEmpty(productSearchVo.getEnd_time()) || "".equals(productSearchVo.getEnd_time())){
            productSearchVo.setEnd_time(null);
        }
        if(StringUtils.isEmpty(productSearchVo.getStart_time()) || "".equals(productSearchVo.getStart_time())){
            productSearchVo.setStart_time(null);
        }
        List<Product> list  = null;
        int total_count  = productMapper.queryTotalCountProduct(productSearchVo);
        if(total_count > 0) {
            list = productMapper.queryProductList(productSearchVo);
        }else{
            list = new ArrayList<>();
        }
        boolean isHasMore = true;
        if(rows == 0){
            isHasMore = total_count > (productSearchVo.getPage() + 1) * CommonConstants.PAGE_SIZE;
        }else {
            isHasMore = total_count > (productSearchVo.getPage() + 1) * rows;
        }
        if (total_count == 0){
            return new PageResult<>(total_count,isHasMore, Collections.emptyList());
        }
        return new PageResult<>(total_count,isHasMore,list);
    }
}
